package com.hhc.service.impl;

import com.hhc.dto.RestResult;
import com.hhc.dto.TeacherCourseAssignmentDTO;
import com.hhc.entity.TeacherCourseAssignment;
import com.hhc.query.CourseTeacherQuery;
import com.hhc.query.TeacherCourseAssignmentQuery;
import com.hhc.repository.TeacherCourseAssignmentRepository;
import com.hhc.service.TeacherCourseAssignmentService;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *   教师授课任务业务逻辑实现类
 * </p>
 * @author pc
 */
@Service
public class TeacherCourseAssignmentServiceImpl implements TeacherCourseAssignmentService {
    @Autowired
    private TeacherCourseAssignmentRepository teacherCourseAssignmentRepository;

    @Override
    @Transactional
    public RestResult<TeacherCourseAssignmentDTO> addTeacherCourseAssignment(TeacherCourseAssignmentDTO assignmentDTO) {
        if (teacherCourseAssignmentRepository.existsByTeacherIdAndCourseId(assignmentDTO.getTeacherId(), assignmentDTO.getCourseId())) {
            return RestResult.failed("该教师已关联此课程");
        }
        TeacherCourseAssignment newAssignment = new TeacherCourseAssignment();
        newAssignment.setTeacherId(assignmentDTO.getTeacherId());
        newAssignment.setCourseId(assignmentDTO.getCourseId());
        newAssignment.setSemester(assignmentDTO.getSemester());
        newAssignment.setClassSchedule(assignmentDTO.getClassSchedule());
        newAssignment.setCreateBy("System");
        newAssignment.setUpdateBy("System");

        TeacherCourseAssignment savedAssignment = teacherCourseAssignmentRepository.save(newAssignment);
        return RestResult.success(convertToDTO(savedAssignment));
    }

    @Override
    @Transactional
    public RestResult<Void> deleteTeacherCourseAssignment(Long id) {
        Optional<TeacherCourseAssignment> teacherCourseAssignmentOptional = teacherCourseAssignmentRepository.findById(id);
        if (teacherCourseAssignmentOptional.isEmpty()) {
            return RestResult.failed("授课任务不存在");
        }
        teacherCourseAssignmentRepository.deleteById(id);
        return RestResult.success("");
    }

    @Override
    @Transactional
    public RestResult<TeacherCourseAssignmentDTO> updateTeacherCourseAssignment(TeacherCourseAssignmentDTO assignmentDTO) {
        Optional<TeacherCourseAssignment> teacherCourseAssignmentOptional = teacherCourseAssignmentRepository.findById(assignmentDTO.getId());
        if (teacherCourseAssignmentOptional.isEmpty()) {
            return RestResult.failed("授课任务不存在");
        }

        TeacherCourseAssignment existingAssignment = teacherCourseAssignmentOptional.get();
        existingAssignment.setTeacherId(assignmentDTO.getTeacherId());
        existingAssignment.setCourseId(assignmentDTO.getCourseId());
        existingAssignment.setSemester(assignmentDTO.getSemester());
        existingAssignment.setClassSchedule(assignmentDTO.getClassSchedule());
        existingAssignment.setUpdateBy("System");

        TeacherCourseAssignment updatedAssignment = teacherCourseAssignmentRepository.save(existingAssignment);
        return RestResult.success(convertToDTO(updatedAssignment));
    }

    @Override
    public RestResult<List<TeacherCourseAssignmentDTO>> getTeacherCourseAssignmentList(TeacherCourseAssignmentQuery query) {
        List<TeacherCourseAssignment> assignments = teacherCourseAssignmentRepository.findByTeacherIdAndSemester(query.getTeacherId(), query.getSemester());

        return RestResult.success(assignments.stream().map(this::convertToDTO).collect(Collectors.toList()));
    }

    @Override
    public RestResult<List<TeacherCourseAssignmentDTO>> getCourseTeachers(CourseTeacherQuery query) {
        List<TeacherCourseAssignment> assignments = teacherCourseAssignmentRepository.findByCourseId(query.getCourseId());
        return RestResult.success(assignments.stream().map(this::convertToDTO).collect(Collectors.toList()));
    }

    private TeacherCourseAssignmentDTO convertToDTO(TeacherCourseAssignment assignmentDO) {
        return new TeacherCourseAssignmentDTO(
                assignmentDO.getId(),
                assignmentDO.getTeacherId(),
                assignmentDO.getCourseId(),
                assignmentDO.getSemester(),
                assignmentDO.getClassSchedule()
        );
    }
}
